#include "kdcmplayercontrolpanel.h"

#include <KLocale>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <KIcon>
#include <KMenu>
#include <QTime>
#include <QFont>
#include <KAction>

#include "kdcmediaplayerconfigpage.h"
#include <kdclib.h>

KDCMPlayerControlPanel::KDCMPlayerControlPanel( QWidget *aParent ) : QWidget( aParent ), pActions( NULL )
{
	KPushButton *p_play = new KPushButton( KIcon("media-playback-start"), "", this );
	p_play->setMaximumSize( 50, p_play->height() );
	connect( p_play, SIGNAL( clicked() ), this, SIGNAL( playClicked() ) );

	KPushButton *p_stop = new KPushButton( KIcon("media-playback-stop"), "", this );
	p_stop->setMaximumSize( 50, p_stop->height() );
	connect( p_stop, SIGNAL( clicked() ), this, SIGNAL( stopClicked() ) );

	KPushButton *p_pause = new KPushButton( KIcon("media-playback-pause"), "", this );
	p_pause->setMaximumSize( 50, p_pause->height() );
	connect( p_pause, SIGNAL( clicked() ), this, SIGNAL( pauseClicked() ) );

	pSlider = new QSlider( Qt::Horizontal );
	connect( pSlider, SIGNAL( sliderReleased() ), this, SLOT( slotSliderReleased() ) );

	pTimeLabel = new QLabel( "00:00:00\n00:00:00" );
	QFont font = pTimeLabel->font();
	font.setPointSize( 8 );
	pTimeLabel->setFont( font );

	QHBoxLayout *hLayout_1 = new QHBoxLayout( this );
	hLayout_1->addWidget( p_play );
	hLayout_1->addWidget( p_stop );
	hLayout_1->addWidget( p_pause );
	hLayout_1->addWidget( pSlider );
	hLayout_1->addWidget( pTimeLabel );
	
	KPushButton *p_video = new KPushButton( i18n("Video driver"), this );
	KMenu *vo_menu = new KMenu( this );
	KSelectAction *actions = new KSelectAction( i18n("Video driver"), this );
	QStringList drivers = KDCMediaPlayerConfigPage::videoDrivers();
	QStringList::const_iterator it;
	for ( it = drivers.constBegin(); it != drivers.constEnd(); ++it )
		actions->addAction( *it );
	vo_menu->addActions( actions->actions() );
	connect( actions, SIGNAL( triggered( QAction * ) ), this, SLOT( slotVideoActionTriggered( QAction * ) ) );
	p_video->setDelayedMenu( vo_menu );
	pAudioBtn = new KPushButton( i18n("Audio track"), this );
	KMenu *ao_menu = new KMenu( this );
	pAudioBtn->setDelayedMenu( ao_menu );
	pActions = new KSelectAction( i18n("Audio track"), this );
	
	hLayout_1->addWidget( p_video );
	hLayout_1->addWidget( pAudioBtn );
}

void KDCMPlayerControlPanel::slotVideoActionTriggered( QAction *aAction )
{
	emit videoDriverChanged( aAction->text() );
}

void KDCMPlayerControlPanel::slotAudioActionTriggered( QAction *aAction )
{
	emit audioIDChanged( aAction->data().toInt() );
}

void KDCMPlayerControlPanel::setSeek( int aValue )
{
	pSlider->setValue( aValue );
	QTime time;
	QString str( "%1\n%2" );
	pTimeLabel->setText( str.arg( time.addSecs( aValue ).toString( "hh:mm:ss" ) ).arg( time.addSecs( pSlider->maximum() ).toString( "hh:mm:ss" ) ) );
}

void KDCMPlayerControlPanel::setAudioTrack( const QList<int> &aList )
{
	pActions->clear();
	pAudioBtn->delayedMenu()->clear();
	KAction *action;
	for ( int i=0; i<aList.size(); ++i )
	{
		action = pActions->addAction( QString::number( i + 1 ) );
		action->setData( QVariant( aList.at( i ) ) );
	}
	pAudioBtn->delayedMenu()->addActions( pActions->actions() );
	connect( pActions, SIGNAL( triggered( QAction * ) ), this, SLOT( slotAudioActionTriggered( QAction * ) ) );
}

void KDCMPlayerControlPanel::setMaxTime( int aValue )
{
	pSlider->setMaximum( aValue );
}

void KDCMPlayerControlPanel::slotSliderReleased()
{
	emit timeSeekChanged( pSlider->value() );
}
